package com.van.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.van.dao.TagDao;
import com.van.pojo.Product;
import com.van.pojo.Tag;
import com.van.serviceImpl.ProductServiceImp;
import com.van.util.MybatisUtil;

/**
 * Servlet implementation class ProductServlet
 */
@WebServlet("/product")
public class ProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private static ProductServiceImp productServiceImp = new ProductServiceImp();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Product> products = productServiceImp.getAllProduct();
		System.out.println(products.size());
		SqlSession session = MybatisUtil.getSession();
		TagDao tagdao = session.getMapper(TagDao.class);
		List<Tag> tags = tagdao.selectAll();
		session.commit();
		request.setAttribute("products", products);
		request.getSession().setAttribute("tags", tags);
		request.getRequestDispatcher("product.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
